/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.data.exchange.runner.proxy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author dell
 */
public class ParserFastJson {

    //record_list
    public static List<Map<String, String>> getContentByType(String jsonString, String type) {
        List<Map<String, String>> mapArray = new ArrayList<>();
        try {
            JSONObject object = JSONObject.parseObject(jsonString);
            JSONArray dataArray = object.getJSONArray(type);
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject jsonObject = dataArray.getJSONObject(i);
                Map<String, String> map = new HashMap<>();
                for (java.util.Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                    String key = entry.getKey();
                    String value = (String) (entry.getValue() + "");
                    map.put(key, value);
                }
                mapArray.add(map);
            }
        } catch (Exception e) {
        }
        return mapArray;
    }

    public static HashMap<String, String> getContentMap(String jsonString) throws Exception {
        HashMap<String, String> map = new HashMap();
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            String key = entry.getKey();
            String value = (String) (entry.getValue() + "");
            map.put(key, value);
        }
        return map;
    }

    public static boolean getReturnStatus(String returnContent) {
        boolean statusFlag = false;
        JSONObject jsonObject = JSONObject.parseObject(returnContent);
        String status = jsonObject.getString("status");
        if ("succeeded".equals(status)) {
            statusFlag = true;
        }
        return statusFlag;
    }

    public static String requestMetadata(List<String> urls, HashMap paramMap) {
        JettyClient jettyClient = new JettyClient();
        String url = getRandomUrl(urls);
        String returnStr = jettyClient.postSendReturnContent(url, paramMap);
        return returnStr;
    }

    public static String getRandomUrl(List<String> urls) {
        int length = urls.size();
        return urls.get((int) (Math.random() * length));
    }
}
